import {
  productCutList,
  loginWithCode,
  cutRegle
} from "../../../api.js"
var app = getApp().globalData
Page({
  data: {
    list: [],
    whether: false,
    countDown: [],
    countDay: '',
    countDownHour: '',
    countDownMinute: '',
    countDownSecond: '',
    page: 1,
    allpage: '',
    onbottom: true,
    xiangqing_alert: true,
    rule_list: []
  },
  onLoad: function (options) {
    cutRegle().then(res => {
      console.log(res)
      this.setData({
        rule_list: res.data.data.regle
      })
    })
  },
  onReady: function () {

  },
  onShow: function () {
    wx.pageScrollTo({
      scrollTop: 0
    })
    getApp().startpage(new Date().getTime())
    this.checkUserInfo()
  },
  //确认是否授权
  checkUserInfo() {
    const xp = this;
    wx.getSetting({
      success: function (res) {
        if (!res.authSetting['scope.userInfo']) {
          wx.navigateTo({
            url: '/pages/public/shouquan/shouquan',
          })
        } else {
          wx.getUserInfo({
            success(res) {
              xp.loginWithCode(res.userInfo)
            }
          })
        }
      }
    })
  },
  //登陆
  loginWithCode(userInfo) {
    const xp = this
    wx.login({
      success({
        code
      }) {
        loginWithCode({
          ...userInfo,
          code,
          business_id: app.businessId
        }).then(res => {
          if (res.data.code === 666) {
            app.Store = res.data.data
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
          xp.index_page()
        })
      }
    })
  },
  index_page() {
    var token = app.Store.token;
    var businessid = app.businessId
    var that = this
    productCutList(token, businessid).then(res => {
      if (res.data.code == 666) {
        var now_time = new Date(res.data.data.now.replace(/\-/g, "/")).getTime()
        var start_time = new Date(res.data.data.activity_start_time.replace(/\-/g, "/")).getTime()
        var end_time = new Date(res.data.data.activity_end_time.replace(/\-/g, "/")).getTime()
        if (now_time >= start_time) {
          that.data.whether = true
          that.countDown(res.data.data.activity_end_time, res.data.data.now)
        } else {
          that.countDown(res.data.data.activity_start_time, res.data.data.now)
        }
        var mun = res.data.data.count - (parseInt(res.data.data.count / res.data.data.perpage) * res.data.data.perpage) == 0 ? parseInt(res.data.data.count / res.data.data.perpage) : parseInt(res.data.data.count / res.data.data.perpage) + 1
        that.setData({
          list: res.data.data.cut_product_list,
          whether: that.data.whether,
          allpage: mun,
          page: res.data.data.page
        })
      }
    })
  },
  onHide: function () {
    getApp().endpage(new Date().getTime())
  },
  goproduct(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/public/productdetail/productdetail?id=' + id,
    })
  },
  // 倒计时
  countDown(endTime, nowTime) {
    var now = new Date(nowTime.replace(/\-/g, "/")).getTime()
    var end_time = new Date(endTime.replace(/\-/g, "/")).getTime()
    var totalSecond = (end_time - now) / 1000
    var interval = setInterval(function () {
      var second = totalSecond;
      var day = Math.floor((second) / 3600 / 24);
      var daStr = day.toString();
      var hr = Math.floor((second) / 3600);
      var hrStr = hr.toString() % 24;
      if (hrStr.length == 1) hrStr = '0' + hrStr;
      var min = Math.floor((second - hr * 3600) / 60);
      var minStr = min.toString();
      if (minStr.length == 1) minStr = '0' + minStr;
      // var sec = second - hr * 3600 - min * 60;
      // var secStr = sec.toString();
      // if (secStr.length == 1) secStr = '0' + secStr;
      this.setData({
        countDay: daStr,
        countDownHour: hrStr,
        countDownMinute: minStr,
        // countDownSecond: secStr,
      });
      totalSecond--;
      if (totalSecond < 0) {
        clearInterval(interval);
        // wx.showToast({
        //   title: '活动已结束',
        // });
        this.setData({
          countDay: '0',
          countDownHour: '00',
          countDownMinute: '00',
          // countDownSecond: '00',
          // jieshu: true
        });
      }
    }.bind(this), 1000);
  },
  goactiv(e) {
    var id = e.currentTarget.dataset.id
    var index = e.currentTarget.dataset.index
    wx.navigateTo({
      url: '/pages/activity/details/details?id=' + id + '&enter_type=7&label=1&position=' + index,
    })
  },
  onReachBottom() {
    var token = app.Store.token;
    var businessid = app.businessId
    wx.showLoading({
      title: '正在加载...',
    })
    var that = this
    if (that.data.page == that.data.allpage) {
      wx.showToast({
        title: '暂无更多商品',
        icon: 'none'
      })
      return
    }
    if (that.data.onbottom) {
      that.setData({
        onbottom: false
      })
    } else {
      return
    }
    productCutList(token, businessid, that.data.page + 1).then(res => {
      if (res.data.code == 666) {
        wx.hideLoading()
        that.setData({
          list: that.data.list.concat(res.data.data.cut_product_list),
          page: res.data.data.page,
          onbottom: true
        })
      }
    })
  },
  golist() {
    wx.navigateTo({
      url: '/pages/activity/myList/myList'
    })
  },
  goindex() {
    wx.switchTab({
      url: '/pages/tabBar/index/index',
    })
  },
  xiangqing() {
    this.setData({
      xiangqing_alert: false
    })
  },
  close_xiangqing() {
    this.setData({
      xiangqing_alert: true
    })
  },
  // 定义分享
  onShareAppMessage(res) {
    return {
      title: "芮享邀你来砍价，购买奢侈品，最低只要600起",
      imageUrl: "https://document.growthupup.com/share310.png?t=" + new Date().getTime(),
      path: '/pages/activity/list/list',
    }
  },
})